home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Resources / Developers / XAMPP 1.5.4 / Windows installer / xampp-win32-1.5.4-installer.exe / xampp / mysql / scripts / mysqld_safe < prev    next >
Encoding:
Text File  |  2006-04-26  |  12.6 KB  |  427 lines

  1. #!/bin/sh
  2. # Copyright Abandoned 1996 TCX DataKonsult AB & Monty Program KB & Detron HB
  3. # This file is public domain and comes with NO WARRANTY of any kind
  4. #
  5. # scripts to start the MySQL daemon and restart it if it dies unexpectedly
  6. #
  7. # This should be executed in the MySQL base directory if you are using a
  8. # binary installation that has other paths than you are using.
  9. #
  10. # mysql.server works by first doing a cd to the base directory and from there
  11. # executing mysqld_safe
  12.  
  13. KILL_MYSQLD=1;
  14. MYSQLD=
  15.  
  16. trap '' 1 2 3 15            # we shouldn't let anyone kill us
  17.  
  18. umask 007
  19.  
  20. defaults=
  21. case "$1" in
  22.     --no-defaults|--defaults-file=*|--defaults-extra-file=*)
  23.       defaults="$1"; shift
  24.       ;;
  25. esac
  26.  
  27. usage () {
  28.         cat <<EOF
  29. Usage: $0 [OPTIONS]
  30.   --no-defaults              Don't read the system defaults file
  31.   --defaults-file=FILE       Use the specified defaults file
  32.   --defaults-extra-file=FILE Also use defaults from the specified file
  33.   --ledir=DIRECTORY          Look for mysqld in the specified directory
  34.   --log-error=FILE           Log errors to the specified log file
  35.   --open-files-limit=LIMIT   Limit the number of open files
  36.   --core-file-size=LIMIT     Limit core files to the specified size
  37.   --timezone=TZ              Set the system timezone
  38.   --mysqld=FILE              Use the specified file as mysqld
  39.   --mysqld-version=VERSION   Use "mysqld-VERSION" as mysqld
  40.   --nice=NICE                Set the scheduling priority of mysqld
  41.   --skip-kill-mysqld         Don't try to kill stray mysqld processes
  42.  
  43. All other options are passed to the mysqld program.
  44.  
  45. EOF
  46.         exit 1
  47. }
  48.  
  49.  
  50. parse_arguments() {
  51.   # We only need to pass arguments through to the server if we don't
  52.   # handle them here.  So, we collect unrecognized options (passed on
  53.   # the command line) into the args variable.
  54.   pick_args=
  55.   if test "$1" = PICK-ARGS-FROM-ARGV
  56.   then
  57.     pick_args=1
  58.     shift
  59.   fi
  60.  
  61.   for arg do
  62.     case "$arg" in
  63.       --skip-kill-mysqld*)
  64.         KILL_MYSQLD=0;
  65.         ;;
  66.       # these get passed explicitly to mysqld
  67.       --basedir=*) MY_BASEDIR_VERSION=`echo "$arg" | sed -e "s;--basedir=;;"` ;;
  68.       --datadir=*) DATADIR=`echo "$arg" | sed -e "s;--datadir=;;"` ;;
  69.       --pid-file=*) pid_file=`echo "$arg" | sed -e "s;--pid-file=;;"` ;;
  70.       --user=*) user=`echo "$arg" | sed -e "s;--[^=]*=;;"` ; SET_USER=1 ;;
  71.  
  72.       # these two might have been set in a [mysqld_safe] section of my.cnf
  73.       # they are added to mysqld command line to override settings from my.cnf
  74.       --socket=*)  mysql_unix_port=`echo "$arg" | sed -e "s;--socket=;;"` ;;
  75.       --port=*)    mysql_tcp_port=`echo "$arg" | sed -e "s;--port=;;"` ;;
  76.  
  77.       # mysqld_safe-specific options - must be set in my.cnf ([mysqld_safe])!
  78.       --ledir=*)   ledir=`echo "$arg" | sed -e "s;--ledir=;;"` ;;
  79.       --log-error=*) err_log=`echo "$arg" | sed -e "s;--log-error=;;"` ;;
  80.       --open-files-limit=*) open_files=`echo "$arg" | sed -e "s;--open-files-limit=;;"` ;;
  81.       --core-file-size=*) core_file_size=`echo "$arg" | sed -e "s;--core-file-size=;;"` ;;
  82.       --timezone=*) TZ=`echo "$arg" | sed -e "s;--timezone=;;"` ; export TZ; ;;
  83.       --mysqld=*)   MYSQLD=`echo "$arg" | sed -e "s;--mysqld=;;"` ;;
  84.       --mysqld-version=*)
  85.     tmp=`echo "$arg" | sed -e "s;--mysqld-version=;;"`
  86.     if test -n "$tmp"
  87.     then
  88.       MYSQLD="mysqld-$tmp"
  89.     else
  90.       MYSQLD="mysqld"
  91.     fi
  92.     ;;
  93.       --nice=*) niceness=`echo "$arg" | sed -e "s;--nice=;;"` ;;
  94.       --help)
  95.         usage
  96.         ;;
  97.       *)
  98.         if test -n "$pick_args"
  99.         then
  100.           # This sed command makes sure that any special chars are quoted,
  101.           # so the arg gets passed exactly to the server.
  102.           args="$args "`echo "$arg" | sed -e 's,\([^a-zA-Z0-9_.-]\),\\\\\1,g'`
  103.         fi
  104.         ;;
  105.     esac
  106.   done
  107. }
  108.  
  109.  
  110. #
  111. # First, try to find BASEDIR and ledir (where mysqld is)
  112.  
  113. MY_PWD=`pwd`
  114. # Check for the directories we would expect from a binary release install
  115. if test -f ./share/mysql/english/errmsg.sys -a -x ./bin/mysqld
  116. then
  117.   MY_BASEDIR_VERSION=$MY_PWD        # Where bin, share and data are
  118.   ledir=$MY_BASEDIR_VERSION/bin        # Where mysqld is
  119. # Check for the directories we would expect from a source install
  120. elif test -f ./share/mysql/english/errmsg.sys -a \
  121.  -x ./libexec/mysqld
  122. then
  123.   MY_BASEDIR_VERSION=$MY_PWD        # Where libexec, share and var are
  124.   ledir=$MY_BASEDIR_VERSION/libexec    # Where mysqld is
  125. # Since we didn't find anything, used the compiled-in defaults
  126. else
  127.   MY_BASEDIR_VERSION=/usr/local
  128.   ledir=/usr/local/libexec
  129. fi
  130.  
  131. #
  132. # Second, try to find the data directory
  133. #
  134.  
  135. # Try where the binary installs put it
  136. if test -d $MY_BASEDIR_VERSION/data/mysql
  137. then
  138.   DATADIR=$MY_BASEDIR_VERSION/data
  139.   if test -z "$defaults" -a -r "$DATADIR/my.cnf"
  140.   then
  141.     defaults="--defaults-extra-file=$DATADIR/my.cnf"
  142.   fi
  143. # Next try where the source installs put it
  144. elif test -d $MY_BASEDIR_VERSION/var/mysql
  145. then
  146.   DATADIR=$MY_BASEDIR_VERSION/var
  147. # Or just give up and use our compiled-in default
  148. else
  149.   DATADIR=/usr/local/var
  150. fi
  151.  
  152. if test -z "$MYSQL_HOME"
  153. then 
  154.   if test -r "$MY_BASEDIR_VERSION/my.cnf" && test -r "$DATADIR/my.cnf"
  155.   then
  156.     echo "WARNING: Found two instances of my.cnf -"
  157.     echo "$MY_BASEDIR_VERSION/my.cnf and"
  158.     echo "$DATADIR/my.cnf"
  159.     echo "IGNORING $DATADIR/my.cnf"
  160.     echo
  161.     MYSQL_HOME=$MY_BASEDIR_VERSION
  162.   elif test -r "$DATADIR/my.cnf"
  163.   then
  164.     echo "WARNING: Found $DATADIR/my.cnf"
  165.     echo "Datadir is deprecated place for my.cnf, please move it to $MY_BASEDIR_VERSION"
  166.     echo
  167.     MYSQL_HOME=$DATADIR
  168.   else
  169.     MYSQL_HOME=$MY_BASEDIR_VERSION
  170.   fi
  171. fi
  172. export MYSQL_HOME
  173.  
  174. user=mysql
  175. niceness=0
  176.  
  177. # these rely on $DATADIR by default, so we'll set them later on
  178. pid_file=
  179. err_log=
  180.  
  181. # Get first arguments from the my.cnf file, groups [mysqld] and [mysqld_safe]
  182. # and then merge with the command line arguments
  183. if test -x ./bin/my_print_defaults
  184. then
  185.   print_defaults="./bin/my_print_defaults"
  186. elif test -x /usr/local/bin/my_print_defaults
  187. then
  188.   print_defaults="/usr/local/bin/my_print_defaults"
  189. elif test -x /usr/local/bin/mysql_print_defaults
  190. then
  191.   print_defaults="/usr/local/bin/mysql_print_defaults"
  192. else
  193.   print_defaults="my_print_defaults"
  194. fi
  195.  
  196. args=
  197. SET_USER=2
  198. parse_arguments `$print_defaults $defaults --loose-verbose mysqld server`
  199. if test $SET_USER -eq 2
  200. then
  201.   SET_USER=0
  202. fi
  203. parse_arguments `$print_defaults $defaults --loose-verbose mysqld_safe safe_mysqld`
  204. parse_arguments PICK-ARGS-FROM-ARGV "$@"
  205. safe_mysql_unix_port=${mysql_unix_port:-${MYSQL_UNIX_PORT:-/tmp/mysql.sock}}
  206.  
  207. # Make sure that directory for $safe_mysql_unix_port exists
  208. mysql_unix_port_dir=`dirname $safe_mysql_unix_port`
  209. if [ ! -d $mysql_unix_port_dir ]
  210. then
  211.   mkdir $mysql_unix_port_dir
  212.   chown $user $mysql_unix_port_dir
  213. fi
  214.  
  215. # Use the mysqld-max binary by default if the user doesn't specify a binary
  216. if test -z "$MYSQLD"
  217. then
  218.   if test -x $ledir/mysqld-max
  219.   then
  220.     MYSQLD=mysqld-max
  221.   else
  222.     MYSQLD=mysqld
  223.   fi
  224. fi
  225.  
  226. if test ! -x $ledir/$MYSQLD
  227. then
  228.   echo "The file $ledir/$MYSQLD doesn't exist or is not executable"
  229.   echo "Please do a cd to the mysql installation directory and restart"
  230.   echo "this script from there as follows:"
  231.   echo "./bin/mysqld_safe".
  232.   echo "See http://dev.mysql.com/doc/mysql/en/mysqld_safe.html for more"
  233.   echo "information"
  234.   exit 1
  235. fi
  236.  
  237. if test -z "$pid_file"
  238. then
  239.   pid_file=$DATADIR/`/bin/hostname`.pid
  240. else
  241.   case "$pid_file" in
  242.     /* ) ;;
  243.     * )  pid_file="$DATADIR/$pid_file" ;;
  244.   esac
  245. fi
  246. test -z "$err_log"  && err_log=$DATADIR/`/bin/hostname`.err
  247.  
  248. if test -n "$mysql_unix_port"
  249. then
  250.   args="--socket=$mysql_unix_port $args"
  251. fi
  252. if test -n "$mysql_tcp_port"
  253. then
  254.   args="--port=$mysql_tcp_port $args"
  255. fi
  256.  
  257. if test $niceness -eq 0
  258. then
  259.   NOHUP_NICENESS="nohup"
  260. else
  261.   NOHUP_NICENESS="nohup nice -$niceness"
  262. fi
  263.  
  264. # Using nice with no args to get the niceness level is GNU-specific.
  265. # This check could be extended for other operating systems (e.g.,
  266. # BSD could use "nohup sh -c 'ps -o nice -p $$' | tail -1").
  267. # But, it also seems that GNU nohup is the only one which messes
  268. # with the priority, so this is okay.
  269. if nohup nice > /dev/null 2>&1
  270. then
  271.     normal_niceness=`nice`
  272.     nohup_niceness=`nohup nice`
  273.  
  274.     numeric_nice_values=1
  275.     for val in $normal_niceness $nohup_niceness
  276.     do
  277.         case "$val" in
  278.             -[0-9] | -[0-9][0-9] | -[0-9][0-9][0-9] | \
  279.              [0-9] |  [0-9][0-9] |  [0-9][0-9][0-9] )
  280.                 ;;
  281.             * )
  282.                 numeric_nice_values=0 ;;
  283.         esac
  284.     done
  285.  
  286.     if test $numeric_nice_values -eq 1
  287.     then
  288.         nice_value_diff=`expr $nohup_niceness - $normal_niceness`
  289.         if test $? -eq 0 && test $nice_value_diff -gt 0 && \
  290.             nice --$nice_value_diff echo testing > /dev/null 2>&1
  291.         then
  292.             # nohup increases the priority (bad), and we are permitted
  293.             # to lower the priority with respect to the value the user
  294.             # might have been given
  295.             niceness=`expr $niceness - $nice_value_diff`
  296.             NOHUP_NICENESS="nice -$niceness nohup"
  297.         fi
  298.     fi
  299. else
  300.     if nohup echo testing > /dev/null 2>&1
  301.     then
  302.         :
  303.     else
  304.         # nohup doesn't work on this system
  305.         NOHUP_NICENESS=""
  306.     fi
  307. fi
  308.  
  309. USER_OPTION=""
  310. if test -w / -o "$USER" = "root"
  311. then
  312.   if test "$user" != "root" -o $SET_USER = 1
  313.   then
  314.     USER_OPTION="--user=$user"
  315.   fi
  316.   # If we are root, change the err log to the right user.
  317.   touch $err_log; chown $user $err_log
  318.   if test -n "$open_files"
  319.   then
  320.     ulimit -n $open_files
  321.     args="--open-files-limit=$open_files $args"
  322.   fi
  323.   if test -n "$core_file_size"
  324.   then
  325.     ulimit -c $core_file_size
  326.   fi
  327. fi
  328.  
  329. #
  330. # If there exists an old pid file, check if the daemon is already running
  331. # Note: The switches to 'ps' may depend on your operating system
  332. if test -f $pid_file
  333. then
  334.   PID=`cat $pid_file`
  335.   if /bin/kill -0 $PID > /dev/null 2> /dev/null
  336.   then
  337.     if /bin/ps p $PID | grep -v grep | grep $MYSQLD > /dev/null
  338.     then    # The pid contains a mysqld process
  339.       echo "A mysqld process already exists"
  340.       echo "A mysqld process already exists at " `date` >> $err_log
  341.       exit 1
  342.     fi
  343.   fi
  344.   rm -f $pid_file
  345.   if test -f $pid_file
  346.   then
  347.     echo "Fatal error: Can't remove the pid file: $pid_file"
  348.     echo "Fatal error: Can't remove the pid file: $pid_file at " `date` >> $err_log
  349.     echo "Please remove it manually and start $0 again"
  350.     echo "mysqld daemon not started"
  351.     exit 1
  352.   fi
  353. fi
  354.  
  355. #
  356. # Uncomment the following lines if you want all tables to be automatically
  357. # checked and repaired during startup. You should add sensible key_buffer
  358. # and sort_buffer values to my.cnf to improve check performance or require
  359. # less disk space.
  360. # Alternatively, you can start mysqld with the "myisam-recover" option. See
  361. # the manual for details.
  362. #
  363. # echo "Checking tables in $DATADIR"
  364. # $MY_BASEDIR_VERSION/bin/myisamchk --silent --force --fast --medium-check $DATADIR/*/*.MYI
  365. # $MY_BASEDIR_VERSION/bin/isamchk --silent --force $DATADIR/*/*.ISM
  366.  
  367. echo "Starting $MYSQLD daemon with databases from $DATADIR"
  368.  
  369. # Does this work on all systems?
  370. #if type ulimit | grep "shell builtin" > /dev/null
  371. #then
  372. #  ulimit -n 256 > /dev/null 2>&1        # Fix for BSD and FreeBSD systems
  373. #fi
  374.  
  375. echo "`date +'%y%m%d %H:%M:%S  mysqld started'`" >> $err_log
  376. while true
  377. do
  378.   rm -f $safe_mysql_unix_port $pid_file    # Some extra safety
  379.   if test -z "$args"
  380.   then
  381.     $NOHUP_NICENESS $ledir/$MYSQLD $defaults --basedir=$MY_BASEDIR_VERSION --datadir=$DATADIR $USER_OPTION --pid-file=$pid_file --skip-locking >> $err_log 2>&1
  382.   else
  383.     eval "$NOHUP_NICENESS $ledir/$MYSQLD $defaults --basedir=$MY_BASEDIR_VERSION --datadir=$DATADIR $USER_OPTION --pid-file=$pid_file --skip-locking $args >> $err_log 2>&1"
  384.   fi
  385.   if test ! -f $pid_file        # This is removed if normal shutdown
  386.   then
  387.     echo "STOPPING server from pid file $pid_file"
  388.     break
  389.   fi
  390.  
  391.   if true && test $KILL_MYSQLD -eq 1
  392.   then
  393.     # Test if one process was hanging.
  394.     # This is only a fix for Linux (running as base 3 mysqld processes)
  395.     # but should work for the rest of the servers.
  396.     # The only thing is ps x => redhat 5 gives warnings when using ps -x.
  397.     # kill -9 is used or the process won't react on the kill.
  398.     numofproces=`ps xaww | grep -v "grep" | grep "$ledir/$MYSQLD\>" | grep -c "pid-file=$pid_file"`
  399.  
  400.     echo -e "\nNumber of processes running now: $numofproces" | tee -a $err_log
  401.     I=1
  402.     while test "$I" -le "$numofproces"
  403.     do 
  404.       PROC=`ps xaww | grep "$ledir/$MYSQLD\>" | grep -v "grep" | grep "pid-file=$pid_file" | sed -n '$p'` 
  405.  
  406.       for T in $PROC
  407.       do
  408.         break
  409.       done
  410.       #    echo "TEST $I - $T **"
  411.       if kill -9 $T
  412.       then
  413.         echo "$MYSQLD process hanging, pid $T - killed" | tee -a $err_log
  414.       else 
  415.         break
  416.       fi
  417.       I=`expr $I + 1`
  418.     done
  419.   fi
  420.   echo "`date +'%y%m%d %H:%M:%S'`  mysqld restarted" | tee -a $err_log
  421. done
  422.  
  423. echo "`date +'%y%m%d %H:%M:%S'`  mysqld ended" | tee -a $err_log
  424. echo "" | tee -a $err_log
  425.  
  426.